import java.util.ArrayList;
import java.util.List;

//https://mp.csdn.net/mp_blog/creation/editor?spm=1001.2014.3001.4503
class Solution {
    List<List<Integer>> ret;
    int sum;
    List<Integer> path;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        dfs(candidates, target, 0);
        return ret;
    }
    public void dfs(int[] candidates, int target, int pos) {
        if(sum >= target) {
            if(sum == target) ret.add(new ArrayList<>(path));
            return;
        }
        for(int i = pos; i < candidates.length; i++) {
            sum += candidates[i];
            path.add(candidates[i]);
            //从当前位置开始枚举(因为一个数据可以多次)，不能枚举前面的元素，否则重复
            dfs(candidates, target, i);
            //回溯 -> 清理现场
            sum -= candidates[i];
            path.remove(path.size() - 1);
        }
    }
}